package com.app.aipass.fragment.my.safe;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.app.AppApplication;
import com.app.aipass.app.AppViewModelFactory;
import com.app.aipass.databinding.FragmentSetPasswordBinding;
import com.app.aipass.entity.UserEntity;
import com.app.aipass.utils.StatusBarUtil;
import com.app.aipass.utils.TextUtil;
import com.app.aipass.utils.ToastUtil;
import com.app.aipass.utils.Utils;
import com.app.aipass.view.CustomPopWindow;
import com.app.aipass.view.InputPwdDialog;

import java.util.HashMap;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * @author: woking
 * @date: 2021/6/10
 * @description：
 */
public class SetPasswordFragment extends BaseFragment<FragmentSetPasswordBinding, SafeViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_set_password;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SafeViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(SafeViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.password1Et.setFocusable(false);//让EditText失去焦点，然后获取点击事件
        binding.password2Et.setFocusable(false);//让EditText失去焦点，然后获取点击事件
        binding.password1Et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputPasswordDialog = new InputPwdDialog(getContext(), new InputPwdDialog.InputPasswordListener() {
                    @Override
                    public void callbackPassword(String password) {
//                        Log.e("TAG","====states====callbackPassword=====password=="+password);
                        binding.password1Et.setText(password);
                    }
                });
                inputPasswordDialog.setOnDismissListener(dialog -> {

                });
                inputPasswordDialog.hideDialogOk();
                inputPasswordDialog.show();
            }
        });
        binding.password2Et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputPasswordDialog = new InputPwdDialog(getContext(), new InputPwdDialog.InputPasswordListener() {
                    @Override
                    public void callbackPassword(String password) {
//                        Log.e("TAG","====states====callbackPassword==222===password=="+password);
                        binding.password2Et.setText(password);
                    }
                });
                inputPasswordDialog.setOnDismissListener(dialog -> {

                });
                inputPasswordDialog.hideDialogOk();
                inputPasswordDialog.show();
            }
        });
        binding.saveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!parseInfo())
                {
                    return;
                }
                initPOP(view);

            }
        });
    }
    private CustomPopWindow mCustomPopWindow;
    private InputPwdDialog inputPasswordDialog;
    private void initPOP(View view)
    {
        UserEntity mUserEntity = ((AppApplication)AppApplication.getInstance()).getUserEntity();
        if(mUserEntity == null)
        {
            ToastUtil.showToast("手机号为空");
            return;
        }
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.pop_set_password_send_code,null);
        TextView phone_tv = contentView.findViewById(R.id.phone_tv);
        phone_tv.setText(Utils.hidePhone(getContext(),((AppApplication)AppApplication.getInstance()).getUserEntity().mobile ));
        EditText code_et = contentView.findViewById(R.id.code_et);
        TextView send_code_tv = contentView.findViewById(R.id.send_code_tv);
        TextView tv_queren = contentView.findViewById(R.id.tv_queren);
        send_code_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.smsCode((TextView)view);
            }
        });
        tv_queren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                EditText code_et = contentView.findViewById(R.id.code_et);
                if(TextUtil.isEmpty(code_et.getText().toString()))
                {
                    ToastUtil.showToast("请输入验证码");
                    return;
                }
                mCustomPopWindow.dissmiss();
                HashMap<String, Object> map = new HashMap<>();
                //{"mobile": "","code": "88888888","password": "123456"}
                map.put("payment_password",binding.password1Et.getText().toString());
                map.put("mobile",((AppApplication)AppApplication.getInstance()).getUserEntity().mobile);
                map.put("code",code_et.getText().toString());
                viewModel.reSetUserPassword(map);
            }
        });

        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(getContext())
                .setView(contentView)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.3f) // 控制亮度
                .setFocusable(true)
                .setOutsideTouchable(true)
//                .size(600,400) //设置显示的大小，不设置就默认包裹内容
                .size(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .create()
                .showAtLocation(view, Gravity.BOTTOM,0,20);
//                .showAsDropDown(view,0,20);
    }
    private boolean parseInfo()
    {
        String account = binding.password1Et.getText().toString();
        if(TextUtil.isEmpty(account))
        {
            ToastUtil.showToast("请设置置换密码");
            return false;
        }
        String name = binding.password2Et.getText().toString();
        if(TextUtil.isEmpty(name))
        {
            ToastUtil.showToast("请确认置换密码");
            return false;
        }
        if(!account.equals(name))
        {
            ToastUtil.showToast("置换密码不一致");
            return false;
        }
        return true;
    }
}
